﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using NetCore6WebAPITestOneEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfigurationCommon
{
    public class ConfigurationScopeHelper
    {
        IConfiguration _configuration;
        public AppSettingsModel _appSettingsModel;
        public IOptionsSnapshot<AppSettingsModel> _optionsSnapshot;

        public string _userMysqlConnectionString { get { return _configuration["ConnectionStrings:usermysqlConnectionString"]; }}
        public string _gpMongodbConnectionString { get { return _configuration["ConnectionStrings:gpMongodbConnectionString"]; } }

        public ConfigurationScopeHelper(IConfiguration configuration, IOptions<AppSettingsModel> appSettingsModel, IOptionsSnapshot<AppSettingsModel> optionsSnapshot)
        {
            _configuration = configuration;

            //监测配置文件改变
            ChangeToken.OnChange(() => configuration.GetReloadToken(), () =>
            {
            });


            _appSettingsModel = appSettingsModel.Value;
            _optionsSnapshot = optionsSnapshot;
            
        }

        public AppSettingsModel GetSnapShotValue(string name) {
            return _optionsSnapshot.Get(name);
        }

        public string GetConfigString(string path) {
            return _configuration[path];
        }

        /// <summary>
        /// 通过options获取配置文件对象
        /// 
        /// </summary>
        /// <param name="optionName"></param>
        /// <returns></returns>
        public AppSettingsModel GetOptionObj(string optionName) {
            return _optionsSnapshot.Get(optionName);
        }
    }
}
